package com.nondo.rdp.finance.service.impl.course;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.criteria.Predicate;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.nondo.rdp.auth.endpoint.model.Page;
import com.nondo.rdp.core.common.StatusConstant;
import com.nondo.rdp.finance.domain.course.Course;
import com.nondo.rdp.finance.domain.course.CourseRate;
import com.nondo.rdp.finance.repository.course.CourseRateRepository;
import com.nondo.rdp.finance.repository.course.CourseRepository;
import com.nondo.rdp.finance.service.course.CourseRateService;

@Transactional
@Service
public class CourseRateServiceImpl implements CourseRateService {
	
	@Autowired
	CourseRepository courseRepository;
    @Autowired
    CourseRateRepository courseRateRepository;

    @Override
    public CourseRate findOne(Long id) {
        return this.courseRateRepository.findOne(id);
    }

    @Override
    public CourseRate save(CourseRate courseRate) {
		float rate = 0f;
		courseRate = this.courseRateRepository.save(courseRate);
		Course course = courseRate.getCourse();
		Float avgRate = courseRateRepository.getAvgRate(course.getId());
		if (avgRate != null) {
			rate = avgRate;
		} else {
			rate = courseRate.getRate();
		}
		course.setRate(rate);
		courseRepository.save(course);
		return courseRate;
    }

    @Override
    public void update(CourseRate courseRate) {
        this.courseRateRepository.save(courseRate);
    }
    
    @Override
    public void delete(Long id) {
        this.courseRateRepository.deleteById(id);
    }

    @Override
    public Page<CourseRate> findAll(Page<CourseRate> page, CourseRate courseRate) {
        Sort sort = new Sort(Sort.Direction.DESC, "createDate");
        PageRequest pageRequest = new PageRequest(page.getPageNumber(), page.getPageSize(), sort);
        org.springframework.data.domain.Page<CourseRate> dictPage = courseRateRepository.findAll((root, query, builder) -> {
        	List<Predicate> predicates = new ArrayList<>();
        	if (courseRate != null) {
	            if (StringUtils.isNotEmpty(courseRate.getCourse().getName())) {
	                predicates.add(builder.like(root.get("name"), "%" + courseRate.getCourse().getName() + "%"));
	            }
        	}
            predicates.add(builder.like(root.get("delFlag"), StatusConstant.DEL_FLAG_NORMAL));
            Predicate[] predicatesArray = new Predicate[predicates.size()];
            predicates.toArray(predicatesArray);
        	
            return builder.and(predicatesArray);
        	
        }, pageRequest);

        return new Page<CourseRate>(dictPage);
    }
    
    @Override
	public CourseRate check(Long cid, Long uid) {
		return this.courseRateRepository.findCourseRateByCourseAndUser(cid,uid);
	}
}
